package com.imooc.controller;

import com.imooc.api.intercept.JWTCurrentUserInterceptor;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.pojo.Admin;
import com.imooc.pojo.bo.CreateAdminBO;
import com.imooc.pojo.bo.ResetPwdBO;
import com.imooc.pojo.bo.UpdateAdminBO;
import com.imooc.pojo.vo.AdminInfoVO;
import com.imooc.pojo.vo.AdminVO;
import com.imooc.service.AdminService;
import com.imooc.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 啊昌
 * @date 2025/9/18 22:15
 * AdminInfoController admin crud
 */
@RestController
@Slf4j
@RequestMapping("admininfo")
public class AdminInfoController {
	@Autowired
	private AdminService adminService;


	@PostMapping("list")
	public GraceJSONResult list(String accountName,
	                            Integer page,
	                            Integer limit) {

		if (page == null) page = 1;
		if (limit == null) limit = 10;
		PagedGridResult pagedGridResult = adminService.getAdminList(accountName, page, limit );
		return GraceJSONResult.ok(pagedGridResult);
	}


	@PostMapping("create")
	public GraceJSONResult create(@RequestBody CreateAdminBO createAdminBO) {
		adminService.createAdmin(createAdminBO);
		return GraceJSONResult.ok();
	}

	@PostMapping("delete")
	public GraceJSONResult delete(String username) {
		adminService.deleteAdmin(username);
		return GraceJSONResult.ok();
	}

	@PostMapping("resetPwd")
	public GraceJSONResult resetPwd(@RequestBody  ResetPwdBO resetPwdBO) {
		resetPwdBO.modifyPwd();
		return GraceJSONResult.ok();
	}

	@PostMapping("myInfo")
	public GraceJSONResult myInfo() {
		Admin admin = JWTCurrentUserInterceptor.adminUser.get();
		Admin adminServiceById = adminService.getById(admin.getId());
		AdminInfoVO adminInfoVO = new AdminInfoVO();
		BeanUtils.copyProperties(adminServiceById, adminInfoVO);
		return GraceJSONResult.ok(adminInfoVO);
	}


	@PostMapping("updateMyInfo")
	public GraceJSONResult updateMyInfo(@RequestBody  UpdateAdminBO updateAdminBO) {
		Admin admin = JWTCurrentUserInterceptor.adminUser.get();
		if(admin == null ){
			return GraceJSONResult.errorCustom(ResponseStatusEnum.FAILED);
		}
		updateAdminBO.setId(admin.getId());
		adminService.updateAdmin(updateAdminBO);
		return GraceJSONResult.ok();
	}


}
